/*
 * Copyright (c) 2009-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

/**
 *
 * @author Petr Panteleyev
 */
public class TagListManager {
    private JTextField tagEdit;

    public TagListManager(JTextField tagEdit) {
        this.tagEdit = tagEdit;
    }

    public ArrayList<String> getTags() {
        if (tagEdit.isEnabled()) {
            String tagString = tagEdit.getText();
            tagString = tagString.trim();
            String[] tagArray = tagString.split(",");

            ArrayList<String> tags = new ArrayList<>(tagArray.length);
            for (String t : tagArray) {
                t = t.trim();
                if (t.length() != 0) {
                    tags.add(t);
                }
            }

            return tags;
        } else {
            return null;
        }
    }

    public JMenu buildTagsMenu(ArrayList<? extends Object> tags) {
        if (tags == null || tags.isEmpty()) {
            return null;
        }

        JMenu tagsMenu = new JMenu();

        String lastLtr = "";
        JMenu menu = null;
        JMenuItem lastItem = null;

        for (Object tag : tags) {
            String t = tag.toString();

            /* Just in case */
            if (t.length() == 0) {
                continue;
            }

            JMenuItem item = new JMenuItem(t);
            item.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    onTagsMenuItemSelected(e.getActionCommand());
                }
            });

            String ltr = t.substring(0, 1);

            if (ltr.equals(lastLtr)) {
                if (lastItem != null) {
                    menu = new JMenu(ltr);
                    tagsMenu.remove(lastItem);
                    tagsMenu.add(menu);
                    menu.add(lastItem);
                    lastItem = null;
                }

                menu.add(item);
            } else {
                lastItem = item;
                lastLtr = ltr;
                tagsMenu.add(item);
            }
        }

        tagsMenu.setEnabled(true);
        return tagsMenu;
    }

    private void onTagsMenuItemSelected(String tag) {
        String tStr = tagEdit.getText();

        int pos = tStr.indexOf(tag);
        if (pos != -1) {
            // remove all such tags
            tStr = tStr.replace(tag, "").replaceAll(" +,|, *$|^(, *)*", "");
        } else {
            // add tag
            if (tStr.length() == 0) {
                tStr = tag;
            } else {
                tStr += ", " + tag;
            }
        }

        tagEdit.setText(tStr);
    }

}
